<?php
/*
 *      osdictionary.php
 *      
 *      Copyright 2010 Vitaliy Rakitin <virak@ukr.net>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ositems.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/System/ositem.php");
//require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/DB.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/system.php");
 
class osdictionary {

	private $items = array();
	private $meta;
	private $current = array();
	private $state;// = "isready";
	private $search = array();

	function __construct($meta) {
		$this->meta = $meta;
		//$this->update();
	}


	function additem() {
		$this->state = "isadd";
		$this->current = array();
		return true;
		//return $this->current = new ositem( genuuid(), $this->meta);
	}
	
	function deleteitem($mark = true) {
		if ($mark) {
			
		} else {
			// Удалить запись из базы 
			//unset($this->current);
			$this->current = null;
		}
	}

	
	//function searchbyname() {}
	
	//function searchbyuuid() {}
	
	function searchbyfield($field, $value) {
		if ($metafield = $this->meta->fields->byname($field)) {
			$this->search[$metafield->uuid] = $value;
		} elseif ($metafield = $this->meta->fields->byuuid($field)) {
			$this->search[$metafield->uuid] = $value;
		} else {
			return false;
		}
		$this->update();
		return true;
	}
	
	function cancelsearch() {
		$this->search = array();
		$this->update();
	}
	
/*
	function saveitem() {
		return serialize($this->current);
	}
	
	function openitem($data) {
		$this->current = null;
		$this->current = unserialize($data);
	}
*/
/*
	function __get($name) {
		switch ($name) {
			//case "items": { if (!$this->items) { return new ositems($this->meta);} else {return $this->items;} break;} // Это надо выкинуть из метаданных
			//case "items": { return new ositems($this->meta); break;}
			//case "items": { return $this->items; break;}
			case "name": { return $this->meta->name;break;}
			//case "uuid": { return $this->uuid;break;}
		}
	}

	function __set($name, $value) {
		switch ($name) {
			//case "name": { $this->name = $value;break;}
		}
	}
*/	

	function __get($name) {
		if (is_array($this->items)) {
			foreach ($this->meta->fields->getall() as $metafield) {
				if ($name == $metafield->name) {
					return $this->current[$metafield->uuid];
				}
			}
		}
		
/*
		foreach ($this->meta->tabulars->getall() as $metatabular) {
			if ($name == $metatabular->name) {
				return $this->tabulars[$metatabular->uuid];
			}
		}
*/
		
		switch ($name) {
			case "uuid": {return $this->meta->uuid; break;}
			case "meta": {return $this->meta; break;}
		}
	}
	
	function __set($name, $value) {
		//foreach ($this->fields as $field) {
		//	${$field->meta->name} = $field;
		//}
		//eval($sender->meta->beforechange);
		foreach ($this->meta->fields->getall() as $metafield) {
			if ($name == $metafield->name) {
				eval($metafield->beforechange);
				$this->current[$metafield->uuid] = $value;
				eval($metafield->afterchange);
				return true;
			} 
		}	
	}	
	
	function count() {
		if (!is_array($this->items)) {
			return 0;
		} else {
			return count($this->items);
		}
	}
	
	function first() {
		if ($this->current()) { 
			reset($this->items);
		}
	}
	
	function next() {
		if ($this->current()) { 
			if ($item = next($this->items)) return $this->current = $item; else return $item;
		}
	}
	
	function current() {
		if (is_array($this->items)) {
			return $this->current = current($this->items);
		} else return false;
	}
	
	function prev() {
		if ($this->current()) { 
			if ($item = prev($this->items)) return $this->current = $item; else return $item;
		}
	}
	
	function update() {
		$sql = "SELECT * FROM ".$this->meta->uuid;
		foreach ($this->search as $key=>$value) {
			$sql .= " WHERE $key = '".$value."'";
		}
		$sql .= ";";
		$this->items = $_SESSION["metadata"]->database->select($sql);
		$this->current();
	}
	
	function save() {
		if ($this->state == "isedit") {
			$action = "UPDATE ".$this->meta->uuid." SET";
			foreach ($this->meta->fields->getall() as $metafield) {
				$action .= " ". $metafield->uuid ." = '".addslashes($this->current[$metafield->uuid])."',";
			}
			$action = substr_replace($action, "", -1, 1);
			$action .= " WHERE (id = '".$this->current["id"]."')";
			$action .= $this->owner ? " AND (owner = '".$this->owner."')" : "";
			$action .= ";";
			$_SESSION["metadata"]->database->query($action);
		} elseif ($this->state == "isadd") {
			$action = "INSERT INTO ".$this->meta->uuid."(";
			$action .= " id, ";
			foreach ($this->meta->fields->getall() as $metafield) {
				$action .=  " ". $metafield->uuid.",";
			}
			$action = substr_replace($action, ")", -1, 1);
			$action .= " VALUES('";
			$action .= genUUID()."',";
			foreach ($this->meta->fields->getall() as $metafield) {
				$action .= " '".addslashes($this->current[$metafield->uuid])."',";
			}
			$action = substr_replace($action, ");", -1, 1);
			//echo $action;
			$_SESSION["metadata"]->database->query($action);
		} else {}
		//echo $action;
		$this->state = "isready";
		$this->update();
	}
	
	function cancel() {
		
	}
	
	/*
	private function get() {
		//unset($this->item);
		$sql = "SELECT * FROM ".$this->object->uuid;
		$sql .= $this->owner ? " WHERE owner = '".$this->owner."'" : "";
		$sql .= " ORDER BY ".$this->object->sortby." ".$this->object->direction;
		$sql .= " LIMIT ".$this->object->limit." OFFSET ".$this->object->offset;
		$sql .= ";";
		echo $sql;
		$result = $_SESSION["metadata"]->database->select($sql);
		//echo print_r($result,1);

		foreach ($result as $item) {
			$uuid = $item["id"];
			$this->item[$uuid] = new ositem($this->object, $uuid, $item);
		}

		//return $this->item;
	}
*/
	
/*
	function getallitems() {
		$items = array();
		foreach () {
			$item = new ositem( genuuid(), $this->meta);
		}
	}
/*	
	function __invoke() {
		return $this->getallitems();
	}
*/
}
?>
